/**
 * 
 */
package com.vision.sales.cm.db.data;

import java.io.Serializable;
import java.util.HashSet;
import java.util.Set;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;

import net.sf.gilead.pojo.gwt.LightEntity;

/**
 * @author Mark
 * 
 */
@Entity
@Table(name = Salesman.TABLE)
public class Salesman extends LightEntity implements Serializable {
	
	public static interface FieldSpecs {
		int CODE_MAX = 15;
		int NAME_MAX = 100;
		int ADDRESS_MAX = 500;
		int PHONE_MAX = 60;
		int COMMENT_MAX = 300;
	}
	
	private static final long serialVersionUID = 1838148863341353629L;
	
	// %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

	public static final String TABLE = "salesman"; 
	
	public static final String COL_ID = "id";
	private static final String COL_CODE = "code";
	private static final String COL_NAME = "name";
	private static final String COL_ADDRESS = "address";
	private static final String COL_PHONE = "phone";
	private static final String COL_STATUS = "status";
	private static final String COL_COMMENT = "comment";

	// %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

	@Id
	@GeneratedValue(strategy = GenerationType.AUTO, generator = "ssalesman")
	@SequenceGenerator(name = "ssalesman", sequenceName = "salesman_seq")
	@Column(name = COL_ID)
	private Long id;
	
	@Column(name = COL_CODE, nullable = false, unique = true, updatable = false, length = FieldSpecs.CODE_MAX)
	private String code;
	
	@Column(name = COL_NAME, nullable = false, length = FieldSpecs.NAME_MAX)
	private String name;
	
	@Column(name = COL_ADDRESS, nullable = true, length = FieldSpecs.ADDRESS_MAX)
	private String address;
	
	@Column(name = COL_PHONE, nullable = true, length = FieldSpecs.PHONE_MAX)
	private String phone;
	
	@Column(name = COL_STATUS, nullable = false)
	@Enumerated(EnumType.STRING)
	private SalesmanStatus status;
	
	@Column(name = COL_COMMENT, nullable = true, length = FieldSpecs.COMMENT_MAX)
	private String comment;
	
	@OneToMany(mappedBy = "salesman", fetch = FetchType.LAZY) // TODO Cascade
	private Set<SalesmanProduct> salesmanProducts = new HashSet<SalesmanProduct>();
	
	// %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

	public Salesman() { } // For Serializable

	@Override
	public boolean equals(Object object) {
		if (object instanceof Salesman) {
			Salesman o = (Salesman) object;
			return getId() != null && getId().equals(o.getId());
		}
		return false;
	}
	
	@Override
	public int hashCode() {
		return id == null ? super.hashCode() : id.hashCode();
	}
	
	@Override
	public String toString() {
		return id + ": " + name + " (" + code + ")";
	}
	
	public int compareTo(Salesman salesman) {
		int diff = salesman.getId().compareTo(salesman.getId());
		if (diff != 0) return diff;
		
		return diff;
	}
	
	// %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
	
	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public String getCode() {
		return code;
	}

	public void setCode(String code) {
		this.code = code;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getAddress() {
		return address;
	}

	public void setAddress(String address) {
		this.address = address;
	}

	public String getPhone() {
		return phone;
	}

	public void setPhone(String phone) {
		this.phone = phone;
	}

	public SalesmanStatus getStatus() {
		return status;
	}

	public void setStatus(SalesmanStatus status) {
		this.status = status;
	}

	
	public String getComment() {
		return comment;
	}

	public void setComment(String comment) {
		this.comment = comment;
	}
	
	public Set<SalesmanProduct> getSalesmanProducts() {
		return salesmanProducts;
	}

	public void setSalesmanProducts(Set<SalesmanProduct> salesmanProducts) {
		this.salesmanProducts = salesmanProducts;
	}

	// Inner Classes ***********************************************************

	public enum SalesmanStatus {
		// NOTE When adding new fields, add at the bottom
		ACTIVE("Active"),
		INACTIVE("Inactive");
		
		private String text;
		
		SalesmanStatus(String text){
			this.text = text;
		}
		
		@Override
		public String toString() {
			return this.text;
		}
	}
	
}
